from model import Model, Dao
#from bson.dbref import DBRef


class Category(Model):
    def __init__(self, _dict):
        """Arguments:
            parent_id --- int type, 0 means root
        """
        Model.__init__(self, _dict)
        
        self.name = _dict['name']
        self.description = _dict['description']
        
        # Self associate field
        #self.parent = _dict['parent']
        

    def __repr__(self):
        return '<Category %r>' % (self.name)

class CategoryDao(Dao):
    def __init__(self):
        Dao.__init__(self, Category)
        self.collection = 'categories'

    '''
    def save(self, model):
        """Updates an existing document or inserts a new document, depending on its document parameter.
        Arguments:
            model --- The Model object with id,  if model is a dict type, the dbref field must be DBRef type
        Return:
            The '_id' value of to_save or [None] if manipulate is False and to_save has no _id field.
        """
        # The ID field must exist
        if isinstance(model, dict):
            return self.db.save(self.collection, model)
        else:
            _dict = model.__dict__
            if isinstance(model.parent, Category):
                _dict['parent'] = DBRef('categories', model.parent._id)
            else:
                _dict['parent'] = None # For root
            return self.db.save(self.collection, _dict)
    '''
if __name__ == '__main__':
    # create table
    pass